﻿Imports WiremetModel.Entities.SupplyOrders
Imports WiremetModel.Entities.OEM
Imports InputValidation
Imports InputValidation.GeneralRules
Imports InputValidation.GeneralRules.NumericInputRules
Imports System.ComponentModel

Public Class WireCertificateChemicalCompositionInputDialog

    Private _chemicalComposition As WireCertificateChemicalComposition
    Public Property ChemicalComposition() As WireCertificateChemicalComposition
        Get
            Return _chemicalComposition
        End Get
        Set(ByVal value As WireCertificateChemicalComposition)
            _chemicalComposition = value
        End Set
    End Property


    Private _inputValidator As InputValidator

    Public Sub New()
        InitializeComponent()
        ClearErrorLabels()
        _chemicalComposition = New WireCertificateChemicalComposition()
        _inputValidator = New InputValidator()
    End Sub


    Public Sub New(ByVal chemicalComposition As WireCertificateChemicalComposition)
        InitializeComponent()
        ClearErrorLabels()
        _chemicalComposition = chemicalComposition
        txt_ElementName.Text = _chemicalComposition.ElementName
        txt_ElementAmount.Text = _chemicalComposition.ElementAmount
        _inputValidator = New InputValidator()
    End Sub


    Private Sub ClearErrorLabels()
        lbl_ElementAmountError.Text = String.Empty
        lbl_ElementNameError.Text = String.Empty
    End Sub

#Region "Input validation"

    Private Sub txt_ElementName_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles txt_ElementName.Validating
        Dim notNullable As New NotNullableTextInputValidatingSpecification()
        _inputValidator.ValidateTextBox(txt_ElementName, e, lbl_ElementNameError, notNullable)
    End Sub

    Private Sub txt_ElementName_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles txt_ElementName.Validated
        lbl_ElementNameError.Text = String.Empty
    End Sub

    Private Sub txt_ElementAmount_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles txt_ElementAmount.Validating
        Dim notNullable As New NotNullableTextInputValidatingSpecification()
        _inputValidator.ValidateTextBox(txt_ElementAmount, e, lbl_ElementAmountError, notNullable)
    End Sub

    Private Sub txt_ElementAmount_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles txt_ElementAmount.Validated
        lbl_ElementAmountError.Text = String.Empty
    End Sub

#End Region


    Private Sub btn_Accept_Click(sender As System.Object, e As System.EventArgs) Handles btn_Accept.Click
        Try
            If Not ValidateChildren() Then
                Throw New EntityDataInputException("Podane dane są nieprawidłowe." & vbNewLine &
                                                   "Popraw je i spróbuj ponownie.")
            End If

            Dim confirmationDialogResult As DialogResult = MessageBox.Show("Czy jesteś pewień?", "Uwaga!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
            If confirmationDialogResult = Windows.Forms.DialogResult.Yes Then

                With _chemicalComposition
                    .ElementName = txt_ElementName.Text.Trim()
                    .ElementAmount = txt_ElementAmount.Text.Trim()
                End With

                DialogResult = Windows.Forms.DialogResult.OK
            End If
        Catch ex As EntityDataInputException
            MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
End Class